package com.geeklan.smart.ui.pet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.geeklan.smart.data.model.CaseInfo;
import com.geeklan.smart.data.model.VersionResponse;
import com.geeklan.smart.databinding.AdapterCastListItemBinding;
import com.geeklan.smart.databinding.AdapterFunctionInfoItemBinding;
import com.geeklan.smart.widget.GridSpacingItemDecoration;
import com.geeklan.smart.widget.ItemOnClickListener;

import org.json.JSONArray;

import java.util.List;

public class CaseListAdapter extends RecyclerView.Adapter<CaseListAdapter.ViewHolder> {
    private Context mContext;
    private List<CaseInfo> mData;
    private ItemOnClickListener itemOnClickListener;

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public CaseListAdapter(Context context, List<CaseInfo> data) {
        this.mData = data;
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        AdapterCastListItemBinding adapterCastListItemBinding = AdapterCastListItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(adapterCastListItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CaseInfo caseInfo = mData.get(position);
        holder.mBinding.tvCaseName.setText(caseInfo.getName());
        holder.mBinding.tvCaseInfo.setText(caseInfo.getRemarks());

        try {
            String urlsJson = caseInfo.getPathologyAlbum().replace("\\","");
            JSONArray jsonArray = new JSONArray(urlsJson);
            String[] urls = new String[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                urls[i] = jsonArray.getString(i);
            }

            CasePhotoAdapter photoAdapter = new CasePhotoAdapter(mContext, urls);
            holder.mBinding.rvPhotos.setAdapter(photoAdapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.mBinding.getRoot().setOnClickListener(v -> {
            itemOnClickListener.OnItemClick(v, position);
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    // 自定义 ViewHolder
    static class ViewHolder extends RecyclerView.ViewHolder {
        AdapterCastListItemBinding mBinding;
        public ViewHolder(@NonNull AdapterCastListItemBinding mBinding) {
            super(mBinding.getRoot());
            this.mBinding = mBinding;
            mBinding.rvPhotos.setLayoutManager(new GridLayoutManager(mBinding.getRoot().getContext(), 4)); // 网格布局，3列
            mBinding.rvPhotos.addItemDecoration(new GridSpacingItemDecoration(20));
        }
    }
}
